<template>
  <div class="myscreen">
    <div class="myscreen_title">
      <div :class="{ checkedBgc: value == tabItem }" v-for="(tabItem, index) in tabList" :key="index + 'tabList'"
        @click.stop.prevent="tabClickFn(tabItem, index)">{{ tabItem }}</div>
    </div>
  
    <div class="myscreen_content">
      <Create v-show="flag == 0" :flag="flag" :screenPersonList="screenPersonList" :isUpdateUser="isUpdateUser"
        :oldScreenPersonList="oldScreenPersonList" :list="list" :globalOverScreen="globalOverScreen"
        @getPersonFn="getPersonFn" @onlyScreen="onlyScreen" @overRoomFn="overRoomFn" @closeJoinList="closeJoinList"
        @globalOverScreenFn="globalOverScreenFn"></Create>
      <Join v-show="flag == 1" :flag="flag" :personList="personList" :RoomNo="RoomNo"
        :globalOverScreen="globalOverScreen" @onlyScreen="onlyScreen" @SearchPublicRoom="SearchPublicRoom"
        @globalOverScreenFn="globalOverScreenFn"></Join>
    </div>
  </div>
</template>
<script>
import { mapState, mapMutations } from "vuex";
import Create from "./createScreen";
import Join from "./joinScreen";
export default {
  props: {
    screenPersonList: {
      // 同屏邀请
      type: Array,
      default: () => {
        return [];
      }
    },
    isUpdateUser: {
      type: Boolean,
      default: false
    },
    oldScreenPersonList: {
      // 上一次同屏邀请
      type: Array,
      default: () => {
        return [];
      }
    },
    globalOverScreen: {
      type: Boolean,
      default: false
    },
    sessionList: {
      type: Array,
      default: []
    }
  },
  components: {
    Create,
    Join
  },
  data() {
    return {
      tabList: ["我要同屏", "同屏观看"],
      flag: 0,
      value: "我要同屏",
      list: [],
      personList: [],
      RoomNo: "",
      connectTimes: 0 //重连次数
    };
  },
  computed: {
    ...mapState(["myInfo", "updateUserStatus"]),
    connectionState() {
      return this.$store.state.connectShareScreenServer;
    }
  },
  watch: {
    connectionState(newVal, oldVal) {
      //监听同屏中转服务器连接状态
      console.log("connectionState", newVal, this.reConnect);
      if (!newVal && !this.reConnect && this.connectTimes < 6) {
        this.reConnect = setTimeout(() => {
          this.$shareScreenConnect();
          this.reConnect = null;
          this.connectionState ? (this.connectTimes = 0) : this.connectTimes++;
          console.log("重新连接同屏中转服务器", this.connectionState);
        }, 10000);
      }
    },
    sessionList: {
      handler(newValue, oldValue) {
        // console.log('监听到了',newValue);
        for (let i in this.screenPersonList) {
          for (let j in newValue) {
            if (this.screenPersonList[i].id == newValue[j].id) {
              // console.log('有变！',newValue[j]);
              this.screenPersonList[i]["status"] = newValue[j].status;
            }
          }
        }
      },
      deep: true
    },
    //更改个人在线状态
    updateUserStatus() {
      //会话列表
      for (let i = 0; i < this.screenPersonList.length; i++) {
        if (this.screenPersonList[i].id == this.updateUserStatus.userId) {
          console.log(
            this.updateUserStatus.status,
            this.updateUserStatus.userId,
            this.$getingTime(),
            "同屏列表是否接收到用户在线状态更改=>"
          );
          this.screenPersonList[i].status = this.updateUserStatus.status;
          this.$forceUpdate();
        }
      }
    }
  },
  methods: {
    // 同屏邀请
    getPersonFn(obj) {
      this.$emit("getPersonFn", obj);
    },
    // 点击Tab
    tabClickFn(tabItem, index) {
      if (tabItem == "我要同屏") {
        this.flag = 0;
        this.value = "我要同屏";
      } else if (tabItem == "同屏观看") {
        this.flag = 1;
        this.value = "同屏观看";
        // 获取可进入的房间接口
        this.SearchPublicRoom();
      }
    },
    // 开启房间、加入房间只能选择一个
    onlyScreen(index) {
      if (index != 3) this.flag = index;
      if (index == 0) {
        this.tabList = ["我要同屏"];
        this.value = "我要同屏";
      } else if (index == 1) {
        this.tabList = ["同屏观看"];
        this.value = "同屏观看";
      } else {
        this.tabList = ["我要同屏", "同屏观看"];
      }
    },
    // 更新房间人数
    updataUsers(list) {
      this.list = list;
    },
    // 获取可进入的房间接口
    SearchPublicRoom() {
      let info = this.$ShareScreenAssistance("SearchPublicRoom", []);
      info = JSON.parse(info);
      if (info.return_code === "FALSE") {
        this.$message({
          message: info.return_msg,
          type: "warning"
        });
      }
    },
    // 获取可进入的房间接口返回
    getSearchPublicRoom(Code, Explain, list) {
      console.log(Code, Explain, list, "获取可进入的房间接口返回");
      if (Code == 0) {
        this.personList = JSON.parse(list) || [];
        for (let i in this.personList) {
          if (this.personList[i].OwnerInfo.ClientID == `${this.myInfo.id}_PC`) {
            this.personList.splice(i, 1);
            break;
          }
        }
      } else {
        this.$message({
          message: Explain,
          type: "warning"
        });
      }
    },
    // 清除加入当前房间的人数
    closeJoinList() {
      this.list = [];
      this.globalOverScreenFn();
    },
    // 当前同屏房间关闭时
    overRoomFn(RoomNo) {
      this.RoomNo = RoomNo;
    },
    // 重置全局结束同屏参数
    globalOverScreenFn() {
      this.$emit("globalOverScreenFn");
    },
    // 删除离开房间的人员
    delLeaveRoomUser(ClientID) {
      console.log(this.list, ClientID, "删除离开房间的人员");
      for (let i in this.list) {
        if (this.list[i].ClientID == ClientID) {
          this.list.splice(i, 1);
          break;
        }
      }
    },
    ...mapMutations(["setScreenConfig"]),
    // 进入同屏接口返回//zk0604同屏调整
    getEnterRoom(Code, Explain, ClientID, Users, RoomNo) {
      console.log(Code, Explain, ClientID, Users, RoomNo, "进入同屏接口返回");
      // this.loadingShow = false;
      this.setScreenConfig({ loadingShow: false });
      if (Code == 0) {
        // 更新房间人数 当有人加入时
        if (Users) {
          this.updataUsers(JSON.parse(Users));
          return;
        }
        // this.isInputPWD = false;
        // this.isJoinScreen = true;
        this.setScreenConfig({
          isInputPWD: false,
          isJoinScreen: true
        });
        // this.$emit("onlyScreen", 1);
        // console.log(this.$refs.screen);
        this.screen.onlyScreen(1);
        // 存储链接码
        this.$store.state.screenRoomNo = RoomNo;
      } else {
        this.$message({
          message: Explain,
          type: "warning"
        });
      }
    },
    // 结束同屏接口返回//zk0604同屏调整
    getLeaveRoom(Code, Explain, ClientID) {
      // this.loadingShow = false;
      this.setScreenConfig({ loadingShow: false });
      console.log(Code, Explain, ClientID, "结束同屏接口返回join");
      if (Code == 0) {
        if (ClientID == this.myInfo.id + "_PC") {
          // 结束同屏的人是自己时
          // this.isJoinScreen = false;
          this.setScreenConfig({ isJoinScreen: false });
          // this.$emit("onlyScreen", 3);
          this.screen.onlyScreen(3);
          // init链接码
          this.$store.state.screenRoomNo = null;
        } else {
          // 结束同屏的人是他人时
          // 删除离开房间的人员
          this.delLeaveRoomUser(ClientID);
        }
      } else {
        this.$message({
          message: Explain,
          type: "warning"
        });
      }
    }
  },
  created() {
    console.log("screenCreated");
    // 获取可进入的房间接口返回
    window.getSearchPublicRoom = this.getSearchPublicRoom;
    // 新增外壳方法抽离
    window.getEnterRoom = this.getEnterRoom;
    window.getLeaveRoom = this.getLeaveRoom;

    // 注册同屏
    this.$shareScreenConnect();
  }
};
</script>
<style lang="less" scoped>
.myscreen {
  width: 100%;
  height: 100%;
  padding-bottom: 16px;
  background: #f5f5f5;
  display: flex;
  flex-direction: column;

  .myscreen_title {
    width: 100%;
    height: 48px;
    display: flex;
    align-items: center;
    padding-left: 10px;
    background: #ffffff;

    div {
      width: 112px;
      height: 48px;
      line-height: 48px;
      text-align: center;
      margin-right: 20px;
      font-size: 14px;
      color: #333333;
      cursor: pointer;
    }

    .checkedBgc {
      border-bottom: 3px solid #fa7500;
    }
  }

  .myscreen_content {
    flex: 1;
    display: flex;
    flex-direction: column;
  }
}
</style>
